package com.qd.panda.wallet.user;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.exception.ApiException;
import com.cdqidi.util.page.PageUtil;
import com.qd.common.flow.domain.approval.FlowDataDTO;
import com.qd.common.panda.domain.entity.wallet.user.UserWalletRefundApplyDTO;
import com.qd.common.sys.annotation.OperatorInfo;
import com.qd.common.sys.enums.OperatorType;
import com.qd.common.sys.enums.RemoteEnum;
import com.qd.common.sys.util.SecurityUtils;
import com.qd.panda.service.wallet.user.UserWalletRefundApplyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;

/**
 * 钱包/用户钱包/WEB_退款处理
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/sys/wallet/userWallet/")
@Slf4j
public class UserWalletRefundApplyController {
    private final UserWalletRefundApplyService userWalletRefundApplyService;

    /**
     * 待审批列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @PreAuthorize("@ps.hasPermission('system:userWalletRefundApplyOrder:refundFlowPage')")
    @OperatorInfo(title = "退款审批列表", operator = OperatorType.OTHER)
    @GetMapping(value = "/refundFlowPage")
    public IPage<FlowDataDTO<UserWalletRefundApplyDTO>> refundFlowPage(UserWalletRefundApplyDTO dto) {
        if (!SecurityUtils.getLoginUser().getRemote().equals(RemoteEnum.WEB.getCode())) {
            throw new ApiException(401, "没有权限操作");
        }
        return userWalletRefundApplyService.flowPage(PageUtil.buildPage(dto), dto);
    }

    /**
     * 列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/page")
    public IPage<UserWalletRefundApplyDTO> page(UserWalletRefundApplyDTO dto) {
        if (!SecurityUtils.getLoginUser().getRemote().equals(RemoteEnum.WEB.getCode())) {
            throw new ApiException(401, "没有权限操作");
        }
        return userWalletRefundApplyService.page(PageUtil.buildPage(dto), dto);
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/getById")
    public UserWalletRefundApplyDTO getById(@Validated @NotBlank(message = "ID不能为空") String id) {
        return userWalletRefundApplyService.getByIdDto(id);
    }

    /**
     * 已审批列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @PreAuthorize("@ps.hasPermission('system:userWalletRefundApplyOrder:refundApprovedPage')")
    @OperatorInfo(title = "退款已审批列表", operator = OperatorType.OTHER)
    @GetMapping(value = "/refundApprovedPage")
    public IPage<FlowDataDTO<UserWalletRefundApplyDTO>> refundApprovedPage(UserWalletRefundApplyDTO dto) {
        if (!SecurityUtils.getLoginUser().getRemote().equals(RemoteEnum.WEB.getCode())) {
            throw new ApiException(401, "没有权限操作");
        }
        return userWalletRefundApplyService.refundApprovedPage(PageUtil.buildPage(dto), dto);
    }
}